import { reqDetailInfo, reqAddShopCart } from '@/api/index.js'
import { getUUID } from '@/utils/uuid'

const state = {
    detailInfo: {},
    // 游客临时身份
    uuid_token: getUUID()
}
const mutations = {
    GETDETAILINFO(state, val) {
        state.detailInfo = val
    }
}
const actions = {
    // 获取产品信息
    async getDetailInfo(context, skuId) {
        const res = await reqDetailInfo(skuId)
        if (res.code === 200) {
            context.commit('GETDETAILINFO', res.data)
        }
    },
    // 添加购物车
    async getAddCart(context, { skuId, skuNum }) {
        const res = await reqAddShopCart(skuId, skuNum)
            // 当前这个函数如果被调用，返回的是一个Promise
        if (res.code === 200) {
            // 成功
            return 'ok'
        } else {
            // 失败
            return Promise.reject(new Error('fail'))
        }
    }
}
const getters = {
    categoryView(state) {
        return state.detailInfo.categoryView
    },
    skuInfo(state) {
        return state.detailInfo.skuInfo
    },
    spuSaleAttrList(state) {
        return state.detailInfo.spuSaleAttrList
    }
}

export default {
    state,
    mutations,
    actions,
    getters
}